﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace EMR.Util
{
    public class MacroUtil
    {
        /// <summary>
        /// 获取宏列表
        /// </summary>
        private static DataTable Macros
        {
            get
            {
                if (_macros == null)
                    _macros = DataAccessFactory.DefaultDataAccess.ExecuteDataTable("select D_NAME,D_COLUMN,D_TABLE,D_SQL,D_TYPE FROM register_item where D_TYPE='宏'");
                return _macros;
            }
        }
        private static DataTable _macros;
        private static string m_PatID;


        public static Dictionary<string, Macro> MacrosList
        {
            get
            {
                if (_macrosList == null)
                {
                    Dictionary<string, Macro> list = new Dictionary<string, Macro>();
                    foreach (DataRow row in Macros.Rows)
                    {
                        Macro mac = new Macro(row);
                        list.Add(mac.Name, mac);
                    }
                    _macrosList = list;
                }
                return _macrosList;
            }
        }

        private static Dictionary<string, Macro> _macrosList;

        private static Dictionary<string, DataRow> _macroSource;
        public static Dictionary<string, DataRow> MacroSource
        {
            get { return _macroSource; }
            set { _macroSource = value;}
        }


        private static void GetMacroSource(string patid)
        {
            _macroSource = new Dictionary<string, DataRow>();
            foreach (DataRow row in Macros.Rows)
            {
                Macro mac = new Macro(row);
                DataTable data = null;
                if (!_macroSource.ContainsKey(mac.D_Table))
                {
                    //此处做一下特殊处理，如果是inpatient 表则传入病人首页序号
                    if (mac.D_Table.Equals("INPATIENT"))
                    {
                        SqlParameter para = new SqlParameter("@NoOfinpat", SqlDbType.VarChar);
                        para.Value = patid;
                        m_PatID = patid;
                        data = DataAccessFactory.DefaultDataAccess.ExecuteDataTable("USP_EMR_GETPATINFO", new SqlParameter[] { para }, CommandType.StoredProcedure);
                        if (data.Rows.Count > 0)
                            _macroSource.Add(mac.D_Table, data.Rows[0]);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(mac.D_Sql)) continue;
                        data = DataAccessFactory.DefaultDataAccess.ExecuteDataTable(mac.D_Sql);
                        if (data.Rows.Count > 0)
                            _macroSource.Add(mac.D_Table, data.Rows[0]);

                    }
                }
            }
        }


        public static void FillMarcValue(string patid, Macro macro)
        {
            if (_macroSource == null || m_PatID != patid/*切换病人后需要重新捞取数据*/)
            {
                GetMacroSource(patid);
            }
            if (!_macroSource.ContainsKey(macro.D_Table)) return;

            DataRow row = _macroSource[macro.D_Table];

            if (IsColumnInTable(row, macro.D_Column))
            {
                if (!row.IsNull(macro.D_Column))
                {
                    macro.MacroValue = row[macro.D_Column].ToString();
                }
                else
                {
                    macro.MacroValue = "";
                }
            }
            else
            {
                macro.MacroValue = "【宏值不存在" + macro.D_Column + "】";;
            }
        }

        /// <summary>
        /// 列是否存在于表中
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        private static bool IsColumnInTable(DataRow dr, string columnName)
        {
            if (dr.Table.Columns.Contains(columnName))
            {
                return true;
            }
            return false;
        }

        public static string FillMarcValue(string patid, string macroName)
        {
            if (_macroSource == null) GetMacroSource(patid);

            //寻找marcor
            if (!MacrosList.ContainsKey(macroName)) return string.Empty;
            Macro macro = MacrosList[macroName];
            FillMarcValue(patid, macro);

            return macro.MacroValue;
        }

    }

    /// <summary>
    /// 宏
    /// </summary>
    public class Macro
    {
        /// <summary>
        /// 名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 类别
        /// </summary>
        public string D_Type { get; set; }
        /// <summary>
        /// 执行语句
        /// </summary>
        public string D_Sql { get; set; }
        /// <summary>
        /// 对应表名
        /// </summary>
        public string D_Table { get; set; }
        /// <summary>
        /// 对应字段
        /// </summary>
        public string D_Column { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        public string MacroValue { get; set; }

        public Macro()
        {

        }

        public Macro(DataRow row)
        {
            Name = row.IsNull("D_NAME") ? string.Empty : row["D_NAME"].ToString();
            D_Table = row.IsNull("D_TABLE") ? string.Empty : row["D_TABLE"].ToString();
            D_Type = row.IsNull("D_TYPE") ? "宏" : row["D_TYPE"].ToString();
            D_Sql = row.IsNull("D_SQL") ? string.Empty : row["D_SQL"].ToString();
            D_Column = row.IsNull("D_COLUMN") ? string.Empty : row["D_COLUMN"].ToString();
        }


    }
}
